//
//  ContentView.swift
//  TheWatermark
//
//  Created by sharui on 2021/6/3.
//

import SwiftUI
import CoreData

struct ContentView: View {
    @Environment(\.managedObjectContext) private var viewContext
    
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
        animation: .default)
    
    
    private var items: FetchedResults<Item>
    @State var simpleImage: NSImage = NSImage(named: "simpleImage")!
    @State var dataSource = WatermarkData()
    @State var imageDataSource = [ImageDataSource]()
    
    var body: some View {
        FileListView(imageDataSource: $imageDataSource)
//        HStack(alignment: .top) {
//            VStack {
//                ScrollView(Axis.Set([.horizontal,.vertical]), showsIndicators: true) {
//                    ZStack {
//                        Image(nsImage:self.simpleImage)
//                            .scaledToFit()
//                            .fixedSize()
//                            .frame(alignment: .center)
//                        Image(nsImage: creatSampleImage(image: self.simpleImage, dataSource: dataSource))
//                            .fixedSize()
//                            .frame(alignment: .center)
//                    }
//                    .frame(width: self.simpleImage.size.width, height: self.simpleImage.size.height)
//                }
//                .frame(maxWidth: self.simpleImage.size.width, maxHeight: self.simpleImage.size.height)
//
//
//                FileListView(imageDataSource: $imageDataSource)
//            }
//
//            VStack(spacing: 10) {
//
//                ActionBarItemForOpenFile(text: $dataSource.path, title: "输出路径")
//                ActionBarItemForInput(text: $dataSource.text, title: "水印文字")
//
//                ActionBarItem(speed:$dataSource.textFontSize, title: "文字大小", range: 10...50)
//                ActionBarItem(speed:$dataSource.textAngle, title: "旋转角度", range: 0...359)
//                ActionBarItem(speed:$dataSource.textHorizontalSpace, title: "横向间距", range: 10...50)
//                ActionBarItem(speed:$dataSource.textVerticalSpace, title: "垂直间距", range: 0...359)
//
//                HStack {
//                    ColorPicker("更改颜色", selection: $dataSource.textColor)
//
//                    Toggle(isOn: $dataSource.randomTextFont) {
//                        Text("随机大小")
//                    }
//
//                    Toggle(isOn: $dataSource.randomTextColor) {
//                        Text("随机颜色")
//                    }
//                }
//                Button("保存") {
//                    downLoad()
//                }
//               Spacer()
//            }
//            .frame(width:350)
//            .padding()
//            .background(Color.sr_color(hex: "0xF5F5F5"))
//        }
    }
    private func downLoad() {
        for imageInfo in self.imageDataSource {
            downImage(image: imageInfo.image, dataSource: dataSource) { image in
                let data = image.tranfrom()!
                FileManager.saveFile(NSData(data: data),path: dataSource.path, name: imageInfo.name)
            }
            
        }
    }
}

private let itemFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateStyle = .short
    formatter.timeStyle = .medium
    return formatter
}()

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
    }
}





